package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import dao.NewsDAO;
import dao.UserDAO;

import beans.News;
import beans.User;

public class AdminNewsController extends MultiActionController{
	
	private NewsDAO newsDAO;
	
	private UserDAO userDAO;
	
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	private LoggedUser loggedUser;

	public void setLoggedUser(LoggedUser loggedUser) {
		this.loggedUser = loggedUser;
	}
	
	public void setNewsDAO(NewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}
	
	public NewsDAO getNewsDAO() {
		return newsDAO;
	}


	public ModelAndView addnews (HttpServletRequest request,
			HttpServletResponse response, News news)throws Exception{
		//ModelMap modelMap = new ModelMap();
		newsDAO.addNews(news);
		return new ModelAndView("redirect:adminnews.htm");
	}
	
	public ModelAndView adminnews (HttpServletRequest request,
			HttpServletResponse response)throws Exception{
		User user = loggedUser.getLoggedUser();
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("user", user);
		modelMap.addAttribute("news", new News());
		modelMap.addAttribute("adminnewsList", newsDAO.DisplayNews());
		return new ModelAndView("adminnews", modelMap);
	}
		

}
